using System;
using System.Collections.Generic;
using System.Text;
using System.Drawing;
using System.Windows.Forms;

namespace cs2335.lab5
{
    public class Sprite: IComponent
    {

        #region Variables
        protected Rectangle container = new Rectangle(0, 0, 100, 100);
        protected StringFormat labelFormat = new StringFormat(StringFormatFlags.NoClip);
        protected SolidBrush backColor = new SolidBrush(Color.FloralWhite);
        protected Font font = new Font("SansSerif", 10);
        protected CCTForm owner;
        protected bool hasFocus = false;
        protected bool hidden = false;
        protected string name;
        protected string text;

        protected Point dragStart;
        
        protected Bitmap image;
        protected string path;
        protected bool dragging;
        #endregion

        #region Properties
        /// <summary>
        /// Tells whether or not a component is hidden. If it is hidden,
        /// it should not be receiving actions.
        /// </summary>
        public bool Hidden
        {
            get { return hidden; }
        }

        public Point Location
        {
            get { return container.Location; }
            set
            {
                container.Location = value;
                Invalidate();
            }
        }

        public Rectangle Bounds
        {
            get { return container; }
            set
            {
                container = value;
                Invalidate();
            }
        }

        public string Name
        {
            get { return name; }
            set { name = value; }
        }

        public System.Drawing.Font Font
        {
            get { return font; }
            set { font = value; Invalidate(); }
        }

        public string Text
        {
            get { return text; }
            set { text = value; Invalidate(); }
        }

        public CCTForm Owner
        {
            get { return owner; }
            set { owner = value; }
        }

        public Color BackColor
        {
            get { return backColor.Color; }
            set { backColor.Color = value; Invalidate(); }
        }
        #endregion

        #region Events
        public event EventHandler Click;
        public event EventHandler GotFocus;
        public event EventHandler KeyPress;
        public event EventHandler KeyDown;
        public event EventHandler LostFocus;
        public event EventHandler Paint;
        #endregion

        #region IComponent Members
        public void Invalidate()
        {
            if (owner != null)
            {
                owner.Invalidate();
            }
        }

        public void Hide()
        {
            hidden = true;
            Invalidate();
        }

        public void Show()
        {
            hidden = false;
            Invalidate();
        }


        public void OnPaint(PaintEventArgs e)
        {
            if (!hidden)
            {
                Graphics g = e.Graphics;
                g.DrawImage(image, container);
                
                //Draw a box around the button to show that it has focus
                if (hasFocus)
                {
                    g.DrawRectangle(Pens.DarkGoldenrod, container);
                }

                if (Paint != null)
                {
                    Paint(this, e);
                }
            }

        }

        public void OnGotFocus(EventArgs e)
        {
            hasFocus = true;
            if (GotFocus != null)
            {
                GotFocus(this, e);
            }
            owner.Invalidate();
        }

        public void OnLostFocus(EventArgs e)
        {
            hasFocus = false;
            if (LostFocus != null)
            {
                LostFocus(this, e);
            }
            owner.Invalidate();
        }


        public void OnMouseClick(System.Windows.Forms.MouseEventArgs e)
        { /*
            if (dragging && e.Button == MouseButtons.Left)
            {
                container.Location = e.Location;
                dragging = false;
                Invalidate();
            }
            else if(e.Button == MouseButtons.Left)
            {
                dragging = true;
            } */
        }

        public void OnMouseUp(System.Windows.Forms.MouseEventArgs e)
        {
            if (dragging)
            {
                container.Location = new Point(container.Location.X + (e.Location.X - dragStart.X),
                                               container.Location.Y + (e.Location.Y - dragStart.Y));
                dragging = false;
                Invalidate();
            }
        }

        public void OnMouseDown(System.Windows.Forms.MouseEventArgs e)
        {
            dragging = true;
            dragStart = new Point(e.Location.X, e.Location.Y);
        }

        public void OnMouseMove(System.Windows.Forms.MouseEventArgs e)
        {
            /* ignore */
        }

        public void OnKeyPress(System.Windows.Forms.KeyPressEventArgs e)
        {
            //Invokes a Click event if the user hits ENTER
            //  while this component has focus
            if (e.KeyChar == '\r')
            {
                if (Click != null)
                {
                    Click(this, e);
                }
            }

            if (KeyPress != null)
            {
                KeyPress(this, e);
            }
        }

        public void OnKeyDown(System.Windows.Forms.KeyEventArgs e)
        {
            if (KeyDown != null)
            {
                KeyDown(this, e);
            }
        }
        #endregion

        public Sprite()
        {
            //path = @"../../../lab5/images/default.bmp";
            path = @"../../../lab5/images/gt.bmp";
            image = new Bitmap(path);
            dragging = false;
        }

        public string Path
        {
            get { return path; }
            set
            {
                path = value;
                Invalidate();
            }
        }

        public Bitmap Image
        {
            get { return image; }
            set 
            { 
                image = value;
                Invalidate();
            }
        }

    }
}
